import { Body, Controller, Get, Post, Res, HttpStatus, UploadedFile, UseInterceptors, HttpException, Query, Request } from '@nestjs/common';
import { WalletFlow } from './walletFlow.entity';
import { WalletFlowService } from './walletFlow.service';
import { WxUserService } from '../wxUser/wxUser.service';
import {
  get,
  post
} from 'request'
import request from 'request'
import {join, resolve} from 'path'
import { createWriteStream } from 'fs';
import { walletFlowEnum } from './walletFlow.enum'

import { ValidTokenInterceptor } from '../../interceptor/validToken'
import {
  existsSync,
  mkdirSync
} from 'fs'

import {
  apibefore
} from '../../router'
import { ValidRSAInterceptor } from 'src/interceptor/validRSA';
import { RedisCacheService } from '../redisCache/redisCache.service';
@Controller(apibefore('/walletFlow'))
@UseInterceptors(ValidTokenInterceptor)
@UseInterceptors(ValidRSAInterceptor)
export class WalletFlowController {
  constructor(private readonly service: WalletFlowService,
    private readonly wxUserService: WxUserService,
    private readonly redisCacheService: RedisCacheService, 
  ) {}

  @Get('/findAll_Cash_RecordList')
  async findAll_Cash_RecordList(@Query() params, @Request() req){
    let wx_user = req.headers.wx_user
    let pageSize: number = Number(params.pageSize || 0)
    let pageIndex: number = Number(params.pageIndex || 0)
    let skip: number = pageSize * pageIndex
    return await this.service.findAll_Cash_RecordList(skip,pageSize,wx_user)
  }

  @Get('/findAll_By_Video')
  findAll_By_Video(@Query() params, @Request() req){
    let pageSize: number = Number(params.pageSize || 0)
    let pageIndex: number = Number(params.pageIndex || 0)
    let skip: number = pageSize * pageIndex
    let video_id = Number(params.video_id)
    let wx_user = req.headers.wx_user
    if(isNaN(video_id)) return new HttpException('非法参数',HttpStatus.INTERNAL_SERVER_ERROR)
    return this.service.findAll_By_Video(skip,pageSize,video_id, wx_user)
  }

  @Get('/findAll_By_User')
  async findAll_By_User(@Query() params, @Request() req){
    let wx_user = req.headers.wx_user
    let pageSize: number = Number(params.pageSize || 0)
    let pageIndex: number = Number(params.pageIndex || 0)
    let cacheKey = `${wx_user}:profit_total:withdrawal:${pageIndex}_${pageSize}`
    let res = await this.redisCacheService.get(cacheKey)
    if(res){
      return res
    }else{
      let skip: number = pageSize * pageIndex
      let data = await this.service.findAll_By_User(skip, pageSize,wx_user)
      await this.redisCacheService.set(cacheKey, data)

      return data
    }
  }
}

